﻿using JGSoft.U9CRTAPIEX.DTOs.Enum;
using JGSoft.U9CRTAPIEX.DTOs.RequestDTOs;
using JGSoft.U9CRTAPIEX.Public;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using UFIDA.U9.CBO.Pub.Controller;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.SM.Ship;
using UFSoft.UBF.AopFrame.Lock;
using UFSoft.UBF.Business;
using UFSoft.UBF.MVC.Common;

namespace JGSoft.U9CRTAPIEX.Controllers
{
    public class OAController : ApiController
    {
        private readonly string _userCode;
        private readonly string _entCode;
        private readonly string _orgCode;
        public OAController()
        {
            JsonConfig _config = new JsonConfig();
            _userCode = _config.SelectToken("UserCode");
            _entCode = _config.SelectToken("EntCode");
            _orgCode = _config.SelectToken("OrgCode");
        }
        /// <summary>
        /// 是否存在出货计划
        /// </summary>
        /// <param name="DocNo"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<bool> ExistsOrder(string DocNo)
        {
            ApiResult<bool> result = new ApiResult<bool>();
            LogManager.WriteLog($"ExistsOrder-{DocNo}", "OARequest");
            using (BPForEngine bp = new BPForEngine())
            {
                ContextDTO tdto = new ContextDTO();
                tdto.UserCode = _userCode;//取接口传入的用户编码
                tdto.EntCode = _entCode;//取接口传入的企业编码
                tdto.CultureName = "zh-CN";//固定
                tdto.OrgCode = _orgCode;//取接口传入的组织编码
                tdto.WriteToContext();
                result.Data = UFIDA.U9.SM.ShipPlan.ShipPlan.Finder.IsExists($"DocNo = '{DocNo}'");
                return result;
            }
        }
        /// <summary>
        /// OA审批出货计划
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<string> ShipPlanOAApprove(ShipPlanOAApprove para)
        {
            ApiResult<string> result = new ApiResult<string>();
            LogManager.WriteLog(JsonConvert.SerializeObject(para), "OARequest");
            using (BPForEngine bp = new BPForEngine())
            {
                ContextDTO tdto = new ContextDTO();
                tdto.UserCode = _userCode;//取接口传入的用户编码
                tdto.EntCode = _entCode;//取接口传入的企业编码
                tdto.CultureName = "zh-CN";//固定
                tdto.OrgCode = _orgCode;//取接口传入的组织编码
                tdto.WriteToContext();
                var shipPlan = UFIDA.U9.SM.ShipPlan.ShipPlan.Finder.Find($"DocNo = '{para.DocNo}'");
                using (ISession session = Session.Open())
                {
                    shipPlan.DescFlexField.PrivateDescSeg6 = para.Status.ToString();
                    session.Commit();
                }
                return result;
            }
        }
        /// <summary>
        /// 设置料品形态属性并生效料品
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<string> SetItemAttribute(SetItemAttributePara para)
        {
            ApiResult<string> result = new ApiResult<string>();
            LogManager.WriteLog(JsonConvert.SerializeObject(para), "OARequest");
            using (BPForEngine bp = new BPForEngine())
            {
                ContextDTO tdto = new ContextDTO();
                tdto.UserCode = _userCode;//取接口传入的用户编码
                tdto.EntCode = _entCode;//取接口传入的企业编码
                tdto.CultureName = "zh-CN";//固定
                tdto.OrgCode = _orgCode;//取接口传入的组织编码
                tdto.WriteToContext();
                //(int)System.Enum.Parse(typeof(ParaItemAttributeEnum), para.materiel_source.Name);
                var Item = ItemMaster.Finder.Find($"Code = '{para.ItemCode}'");
                if(Item == null)
                {
                    result.ResCode = 1;
                    result.ResMsg = $"未找到料号{para.ItemCode}的料品";
                    LogManager.WriteLog(result.ResMsg, "All");
                    return result;
                }
                using (ISession session = Session.Open())
                {
                    Item.ItemFormAttribute = ItemTypeAttributeEnum.GetFromValue((int)System.Enum.Parse(typeof(ParaItemAttributeEnum), para.ItemAttributeName));
                    switch (para.ItemAttributeName)
                    {
                        case "制造件":
                            Item.MrpInfo.FixedLT = 7;
                            break;
                        case "采购件":
                            Item.MrpInfo.FixedLT = 5;
                            break;
                        case "委外加工件":
                            Item.MrpInfo.FixedLT = 5;
                            Item.IsBuildEnable = false;
                            break;
                    }
                    Item.Effective.IsEffective = true;
                    session.Commit();
                }
                return result;
            }
        }

    }
}
